package com.example.isak.myasd.controller;

import java.util.Map;
import java.util.HashMap;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.isak.myasd.common.config.R;
import com.example.isak.myasd.common.utils.Query;
import com.example.isak.myasd.domin.InfoDO;
import com.example.isak.myasd.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 
 * 
 * @author isak
 * @email 1503386669@qq.com
 * @date 2020-10-16 15:34:34
 */
@Controller
@RequestMapping("/controller/info")
public class InfoController{
	@Autowired
	private InfoService infoService;
	
	@ResponseBody
	@PostMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        Page<InfoDO> page = new Page<InfoDO>(query.getPageNo(), query.getPageSize());
		page = infoService.page(page, query);
		// 封装分页数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
        jsonMap.put("rows", page.getRecords());
        jsonMap.put("pageSize", page.getSize());
        jsonMap.put("pageNo", page.getCurrent());
        jsonMap.put("count", page.getTotal());
        return R.success(jsonMap);
	}
	
    @ResponseBody
    @PostMapping("/form")
    public R form(@RequestParam(required = true)String id){
        InfoDO info = infoService.get(id);
        Map<String,Object> jsonMap = new HashMap<String,Object>();
        jsonMap.put("info", info);
        return R.success(jsonMap);
    }
	
	@ResponseBody
	@PostMapping("/save")
	public R save(InfoDO info){
		if(infoService.save(info)>0){
			return R.success();
		}
		return R.error();
	}
	
	@PostMapping("/remove")
	@ResponseBody
	public R remove(String id){
		if(infoService.remove(id)>0){
		    return R.success();
		}
		return R.error();
	}
	
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") String[] ids){
		infoService.batchRemove(ids);
		return R.success();
	}
	
}
